import axios, {AxiosResponse, AxiosRequestConfig} from "axios";
import NProgress from "nprogress"
import {message} from "antd"

NProgress.settings.showSpinner = false
const request = axios.create({
    baseURL: "http://localhost:8080",
    timeout: 5000,
})

// @ts-ignore
request.interceptors.request.use(config => {
    let token = sessionStorage.getItem("token");
    NProgress.start()
    if (token) {
        config.headers["Authorization"] = "DiZ" + token
    }
    return config
})

request.interceptors.response.use(
    res => {

        NProgress.done()

        if (res.status !== 200) {
            // 请求失败
            if (res.status === 401) {
                //无权限
                message.warning("无权限")
            } else if (res.status === 500 || res.status === 505) {
                //无权限
                message.warning("服务器错误")
            } else if (res.status === 404) {
                message.warning("404找不到地址")
            } else {
                message.warning("请求错误")
            }
        }
        if (res.status == 200) {
            return res
        } else {
            return res
        }

    },
    err => {

        NProgress.done()
        message.warning("请求错误")
        console.error(err)
        return err
    }
)

export default request